<?php

declare(strict_types=1);

namespace Modules\Demo\Models;

use Catch\Base\CatchModel as Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Casts\AsCollection;

/**
 * @property $id
 * @property $name
 * @property $img
 * @property $one_file
 * @property $many_files
 * @property $content
 * @property $status
 * @property $dec
 * @property $number
 * @property $type
 * @property $type_api
 * @property $types
 * @property $datetime
 * @property $year
 * @property $month
 * @property $date
 * @property $json_arr
 * @property $parent_id
 * @property $casClass_id
 * @property $creator_id
 * @property $created_at
 * @property $updated_at
 * @property $deleted_at
 * @property $create_time
 * @property $update_time
 */
class Demo extends Model
{


    protected $table = 'k12_demo';

    protected $fillable = ['id', 'name', 'img', 'one_file', 'many_files', 'content', 'status', 'dec', 'number', 'type', 'datetime', 'year', 'month', 'date', 'json_arr', 'parent_id', 'creator_id', 'created_at', 'updated_at', 'deleted_at', 'create_time', 'update_time','type_api','types','casClass_id'];

    /**
     * @var array
     */
    protected array $fields = ['id', 'name', 'img', 'one_file', 'many_files', 'content', 'status', 'dec', 'number', 'type', 'datetime', 'year', 'month', 'date', 'json_arr', 'parent_id', 'created_at', 'updated_at','type_api','types','casClass_id'];

    /**
     * @var array
     */
    protected array $form = ['name', 'img', 'one_file', 'many_files', 'content', 'status', 'dec', 'number', 'type', 'datetime', 'year', 'month', 'date', 'json_arr', 'parent_id','type_api','types','casClass_id'];

    /**
     * @var array
     */
    public array $searchable = [
        'name' => 'like',
        'status' => '=',

    ];

    protected $attributes = [
        'json_arr' => '{"ids":[]}'
    ];

    protected function casts(): array
    {
        return [
            'one_file' => 'array',
            'many_files' => 'array',
            'json_arr' => AsCollection::class,
        ];
    }

    protected function datetime(): Attribute
    {
        return new Attribute(
            get: fn($value) => $value ? : '',
            set: fn($value) => $value ? : null
        );
    }

    protected function year(): Attribute
    {
        return new Attribute(
            get: fn($value) => $value ? (string)$value : '',
            set: fn($value) => $value ? : null
        );
    }

    protected function month(): Attribute
    {
        return new Attribute(
            get: fn($value) => $value ? (string)$value : '',
            set: fn($value) => $value ? : null
        );
    }

    protected function date(): Attribute
    {
        return new Attribute(
            get: fn($value) => $value ? : '',
            set: fn($value) => $value ? : null
        );
    }

    protected function jsonArr(): Attribute
    {
        return new Attribute(
            get: fn($value) => $value ? json_decode($value, true) : [],
            set: fn($value) => $value ? json_encode($value, JSON_UNESCAPED_UNICODE) : json_encode(['ids' => []])
        );
    }

    public function casClass()
    {
        return $this->belongsTo(CasClass::class, 'casClass_id', 'id');
    }
}
